
//非运动模式心率报警
import 'package:ble2301/ble_sdk.dart';
import 'package:ble2301/utils/ble_const.dart';
import 'package:ble2301/utils/device_key.dart';
import 'package:ble2301_example/util/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

import '../util/app_all_value.dart';

class HeartWarnPage extends StatefulWidget{
  const HeartWarnPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _heartWarnState();
  }
}

class _heartWarnState extends State<HeartWarnPage>{
  AppAllValueController controller = Get.find();
  String lowValue="0";
  String highValue="0";
  String respValue="";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text( "非运动模式心率报警",),),
      body: Column(mainAxisSize: MainAxisSize.max,children: [
        Row(children: [
          Text("最低报警心率："),
          DropdownButton(
            value: lowValue,
            items: getMenuList().map((e) => DropdownMenuItem(child: Text(e),value: e,)).toList(), onChanged: (String? value) {
            if(value!=null){
              setState(() {
                lowValue= value;
              });
            }
          },
          ),
          Text("最高报警心率："),
          DropdownButton(
            value: highValue,
            items: getMenuList().map((e) => DropdownMenuItem(child: Text(e),value: e,)).toList(), onChanged: (String? value) {
            if(value!=null){
              setState(() {
                highValue= value;
              });
            }
          },
          )
        ],),
        Row(children: [
          Expanded(child:
          ElevatedButton(
            child: Text('设置'),
            onPressed: (){
              Future.delayed(Duration.zero,(){
                controller.showLoadDialog('同步中'.tr);
                setWarn();
              });

            },
          ))
          ,
          SizedBox(width: 10),
          Expanded(
            child: ElevatedButton(
              child: Text('读取'),
              onPressed: (){
                Future.delayed(Duration.zero,(){
                  controller.showLoadDialog('同步中'.tr);
                  readWarn(0);
                });

              },
            ),
          ),
          SizedBox(width: 10),
          Expanded(
            child: ElevatedButton(
              child: Text('报警指令'),
              onPressed: (){
                Future.delayed(Duration.zero,(){
                  controller.showLoadDialog('同步中'.tr);
                  readWarn(2);
                });

              },
            ),
          ),
        ],),
        Text(respValue.tr)

      ],),
    );
  }

  List<String> getMenuList(){
    List<String> list = [];
    for (int i = 0; i < 256; i++) {
      list.add(i.toString());
    }
    return list;
  }

  @override
  void dispose() {
    super.dispose();
    EventBus().off('dataCallBack');
  }

  @override
  void initState() {
    super.initState();
    EventBus().on('dataCallBack', (arg){
      print('aaa bb '+arg.toString());
      Map map = arg;
      bool finish = map[DeviceKey.End];
      switch (map[DeviceKey.DataType] as String) {
        case BleConst.HEART_WARN_READ:
        case BleConst.HEART_WARN_WRITE:
        case BleConst.HEART_WARN_VALUE:
        if(finish){
          controller.dismissDialog();
          setState(() {
            respValue=arg.toString();
          });
        }
          break;
      }}
    );
  }

  ///0读配置 2报警指令
  void readWarn(int mode){
    controller.writeData(BleSDK.GetHeartWarnDataToMode(mode));
  }

  void setWarn(){
    controller.writeData(BleSDK.SetHeartWarn(int.parse(lowValue),int.parse(highValue)));
  }


}